﻿// UTF-8 with Bom
#include <stdio.h>
#include <Windows.h>
#include <vector>
#include <string>
#include <iostream>
#include "DeviceIoControl.cpp"
using namespace std;
#define GB(x) (x.HighPart << 2) + (x.LowPart >> 20) / 1024.0 // 转换为GB单位显示

void putDrivesType(const char *lpRootPathName);
void putDrivesFreeSpace(const char *lpRootPathName);
void putDrivesInfor(char *lpBuffer);

int main()
{

    int count = 0;
    DWORD dw;
    DWORD dwSize = MAX_PATH;

    char szLogicalDrives[MAX_PATH] = {0};                              // 缓冲区
    DWORD dwResult = GetLogicalDriveStringsA(dwSize, szLogicalDrives); // 获取逻辑驱动器号字符串

    dw = GetLogicalDrives(); // 获取驱动器数
    while (dw != 0)
    {
        count++;
        dw >>= 1;
    }
    if (dwResult > 0 && dwResult <= MAX_PATH) // 处理获取到的结果
    {
        char *szSingleDrive = szLogicalDrives; // 从缓冲区起始地址开始

        while (*szSingleDrive)
        {
            printf("磁盘符号:%s\n", szSingleDrive); // 输出单个驱动器的驱动器号
            wmi_run();
            wmi_getDriveLetters_str(szSingleDrive);
            wmi_close();
            putDrivesType(szSingleDrive); // 输出逻辑驱动器类型
            putDrivesInfor(szSingleDrive);
            putDrivesFreeSpace(szSingleDrive); // 获取磁盘空间信息
            printf("----------------------------\n");
            szSingleDrive += strlen(szSingleDrive) + 1; // 获取下一个驱动器号起始地址
        }
    }
    return 0;
}

void putDrivesType(const char *lpRootPathName)
{
    UINT uDriverType = GetDriveTypeA(lpRootPathName);

    switch (uDriverType)
    {
    case DRIVE_UNKNOWN:
        printf("磁盘类型:未知磁盘\n");
        break;
    case DRIVE_NO_ROOT_DIR:
        printf("磁盘类型:路径无效\n");
        break;
    case DRIVE_REMOVABLE:
        printf("磁盘类型:可移动磁盘\n");
        break;
    case DRIVE_FIXED:
        printf("磁盘类型:固定磁盘\n");
        break;
    case DRIVE_REMOTE:
        printf("磁盘类型:网络磁盘\n");
        break;
    case DRIVE_CDROM:
        printf("磁盘类型:光驱\n");
        break;
    case DRIVE_RAMDISK:
        printf("磁盘类型:映射盘\n");
        break;
    default:
        break;
    }
}

void putDrivesFreeSpace(const char *lpRootPathName)
{
    ULARGE_INTEGER available, total, free; // ULARGE_INTEGER  一个64位的无符号整型值
    if (GetDiskFreeSpaceExA(lpRootPathName, (ULARGE_INTEGER *)&available, (ULARGE_INTEGER *)&total, (ULARGE_INTEGER *)&free))
    {
        printf("总容量  :%.2f GB\n使用空间:%.2f GB\n空闲空间:%.2f GB\n", GB(total), GB(total) - GB(free), GB(free));
    }
}
void putDrivesInfor(char *lpBuffer)
{
    char *p = lpBuffer;
    char volumeNameBuffer[MAX_PATH] = {0};
    char fileSystemNameBuffer[MAX_PATH] = {0};
    DWORD serialNumber = 0;
    DWORD maxComponentLength = 0;
    DWORD fileSystemFlags = 0;
    GetVolumeInformationA(lpBuffer, volumeNameBuffer, MAX_PATH, &serialNumber, &maxComponentLength, &fileSystemFlags, fileSystemNameBuffer, MAX_PATH);
    std::cout << "磁盘名称:" << volumeNameBuffer << std::endl;
    std::cout << "文件系统:" << fileSystemNameBuffer << std::endl;
}
